// pages/chat/chat.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.ossUrl,
    
		replyList: [{
			id: 0,
			text: '有很多超棒的瞬间。'
		},{
			id: 1,
			text: '超棒的！'
		},{
			id: 2,
			text: '好的！'
		},{
			id: 3,
			text: '当然是花酪棒啦！'
		},{
			id: 4,
			text: '必须的～加油加油！'
		},{
			id: 5,
			text: '我当然是超棒的！'
    }],
    
    chatStart: false,
    replyBtnText: '点此回复',
    replyOpen: false,
    replyChoosed: false,
    replyShow: '',

    popShow: true,
    popStartShow: false,
    popGetShow: false,
    randomPiece: 1,
    popGotShow: false,

    popMyShow: false,
    popAudioShow: false,
    popPosterShow: false,
    popRuleShow: false,
    popCompanyShow: false,
    popPrivacyShow: false,

    myAudioList:[{
      id: 0,
      num: '1',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 1,
      num: '2',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 2,
      num: '3',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 3,
      num: '4',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 4,
      num: '5',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    },{
      id: 5,
      num: '6',
      txt: 'XXXXXXXXX',
      src: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3',
      isPlaying: false
    }],

    popPosterList: [{
      id: 0,
      poster: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_img1.png',
      txt: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_txt1.png',
      text: '王一博现场拍摄',
      check: true
    },{
      id: 1,
      poster: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_img2.png',
      txt: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_txt2.png',
      text: '开启时间：05月08日',
      check: false,
      lock: true
    },{
      id: 2,
      poster: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_img2.png',
      txt: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_txt3.png',
      text: '开启时间：05月15日',
      check: false,
      lock: true
    },{
      id: 3,
      poster: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_img2.png',
      txt: 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/pop_poster_txt4.png',
      text: '开启时间：05月22日',
      check: false,
      lock: true
    }],

    voiceImg: 'voice_icon.png',
    voiceImg1: 'voice_icon.png',

    scrollId: '',

    helloIsTap: false,
    yiboIsTap: false,

    privacyChecked: false,

    popTipsShow: true
  },

  popCloseTipsTap(e) {
    this.setData({
      popShow: false,
      popTipsShow: false
    })
  },

  // 我已阅读并同意
  privacyAgreeTap(e) {
    this.setData({
      privacyChecked: true,
      popPrivacyShow: false
    })
  },

  // 关闭隐私声明弹窗
  popClosePrivacyTap(e) {
    this.setData({
      popPrivacyShow: false
    })
  },

  // 显示隐私声明弹窗
  privacyShowTap(e) {
    this.setData({
      popPrivacyShow: true
    })
  },

  // 语音弹层 - 添加小妙
  popAudioTitleTap(e) {
    this.setData({
      popAudioShow: false,
      popCompanyShow: true,
    })
  },

  yiboVoice(e) {
    let that = this;
    this.setData({
      voiceImg1: 'voice_icon.gif',
      yiboIsTap: true
    })
    const innerAudioContext = wx.createInnerAudioContext({
      useWebAudioImplement: true // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
    })
    innerAudioContext.src = 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3';
    innerAudioContext.play() // 播放
    innerAudioContext.onEnded(function() {
      console.log('over!');
      innerAudioContext.destroy();
      that.setData({
        voiceImg1: 'voice_icon.png'
      })
    })
  },

  helloTap(e) {
    let that = this;
    this.setData({
      voiceImg: 'voice_icon.gif',
      helloIsTap: true
    })
    const innerAudioContext = wx.createInnerAudioContext({
      useWebAudioImplement: true // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
    })
    innerAudioContext.src = 'https://evanbuckt.oss-cn-shanghai.aliyuncs.com/milkground/win.mp3';
    innerAudioContext.play() // 播放
    innerAudioContext.onEnded(function() {
      console.log('over!');
      innerAudioContext.destroy();
      that.setData({
        voiceImg: 'voice_icon.png'
      })
    })
  },

  acceptPieceTap(e) {
    wx.navigateTo({
      url: '../index/index',
    })
  },

  getPieceTap(e) {
    let isGot = this.data.popGotShow, random = 0;
    if (isGot) {
      random = Math.floor(Math.random()*4)+1;
    }else{
      random = Math.floor(Math.random()*4)+5;
    }
    this.setData({
      popShow: true,
      popGetShow: true,
      randomPiece: random
    })
  },

  popStartTap(e) {
    this.setData({
      popShow: false,
      popStartShow: false,
      chatStart: true
    })
  },

  replyTap(e) {
    let id = e.currentTarget.id, list = this.data.replyList, txt = '';
    for (let i=0;i<list.length;i++) {
      if (id == list[i].id) {
        txt = list[i].text;
      }
    }
    this.setData({
      replyBtnText: '点此加入聊天',
      replyOpen: false,
      replyChoosed: true,
      replyShow: txt
    })
  },

  replyOpenTap(e) {
    let that = this;
    if (!this.data.replyChoosed){
      this.setData({
        replyOpen: true,
        replyPointShow: false
      })
      setTimeout(function() {
        that.setData({
          scrollId: 'last'
        })
      },3000)
    }
  },

  // 海报弹窗列表点击
  popPosterTap(e) {
    wx.navigateTo({
      url: '../share/share',
    })
  },

  audioListTap(e) {
    let id = e.currentTarget.id, list = this.data.myAudioList, that = this;
    for (let i=0;i<list.length;i++) {
      if (id == list[i].id) {
        const innerAudioContext = wx.createInnerAudioContext({
          useWebAudioImplement: true // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播放频繁的音频建议开启此选项，开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长，因此对于长音频建议关闭此选项
        })
        innerAudioContext.src = list[i].src;
        innerAudioContext.play() // 播放
        list[i].isPlaying = true;
        that.setData({
          myAudioList: list
        })
        innerAudioContext.onEnded(function() {
          console.log('over!');
          innerAudioContext.destroy();
          list[i].isPlaying = false;
          that.setData({
            myAudioList: list
          })
        })
      }
    }
  },

  popMyPosterTap(e) {
    this.setData({
      popMyShow: false,
      popPosterShow: true,
    })
  },

  popMyAudioTap(e) {
    this.setData({
      popMyShow: false,
      popAudioShow: true,
    })
  },

  popCloseTap(e) {
    this.setData({
      popShow: false,
      popStartShow: false,
      popMyShow: false,
      popAudioShow: false,
      popPosterShow: false,
      popRuleShow: false,
      popCompanyShow: false,
    })
  },

  companyTap(e) {
    this.setData({
      popShow: true,
      popCompanyShow: true,
    })
  },

  ruleTap(e) {
    this.setData({
      popShow: true,
      popRuleShow: true,
    })
  },

  myTap(e) {
    this.setData({
      popShow: true,
      popMyShow: true,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})